package com.nari.ies.ne.nw.server.service.fileQuality.impl;

import com.nari.ies.ne.nw.server.entity.FileQualityEntity;
import com.nari.ies.ne.nw.server.mapper.FileQualityMapper;
import com.nari.ies.ne.nw.server.mapper.PlantDataMapper;
import com.nari.ies.ne.nw.server.service.fileQuality.*;
import com.nari.ies.ne.nw.server.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;
import java.text.ParseException;
import java.util.*;

@Slf4j
@Service
public class FileQualityServiceImpl implements IFileQualityService {
    @Autowired
    private PlantDataMapper plantDataMapper;
    @Autowired
    private FileQualityMapper fileQualityMapper;

    @Autowired
    private ITJXXFileQualityService TJXXFileQualityService;
    @Autowired
    private IDQYCFileQualityService DQYCFileQualityService;
    @Autowired
    private ICDQYCFileQualityService CDQYCFileQualityService;
    @Autowired
    private IFDJZFileQualityService FDJZFileQualityService;
    @Autowired
    private INBQ_HLXFileQualityService NBQ_HLXFileQualityService;
    @Autowired
    private IQXHJFileQualityService QXHJFileQualityService;
    @Autowired
    private ISYZXXFileQualityService SYZXXFileQualityService;
    @Autowired
    private IXB_FZFileQualityService XB_FZFileQualityService;

    @Override
    public FileQualityEntity checkFileName(File file) {
        FileQualityEntity fileQuality = new FileQualityEntity();
        if (file == null) {
            return null;
        }
        String result = "";
        // 文件名称
        String fileName = file.getName();
        log.info("文件质量检查------------------" + fileName);
        String plantId = null;
        // FD_GD.GDCZHS_ZTXX-QXHJ_20220329_233000.dat
        //切割文件名
        String[] fileNameArr = fileName.split("_");

        //判断名称格式
       /*if (5 != fileNameArr.length) {
            fileQuality.setFileName(fileName);
            fileQuality.setFileType("");
            fileQuality.setPlantCode("");
            fileQuality.setMessage("文件名称格式有误");
            fileQuality.setForecastDate(new Date());
            fileQuality.setPlantType("");
            fileQuality.setId(null);
            fileQuality.setIsFileDelay(0);
            fileQuality.setIsFileEmpty(0);
            fileQuality.setIsFileMissTag(0);
            fileQuality.setIsFileDefect(0);
            fileQuality.setIsFileFormatError(0);
            fileQuality.setIsFileContentError(0);
        }*/
        String plantType = fileNameArr[0];
        String plantCode = fileNameArr[1];
        String fileType = fileNameArr[2];
        String forecastTime = fileNameArr[3] + fileNameArr[4].substring(0, 4) + "00";

        // 判断日期格式是否正确
        Date forecastDate = null;
        try {
            forecastDate = DateUtil.SDF_DAY_HH_MM_SS.parse(forecastTime);
        } catch (ParseException e) {
            e.printStackTrace();

            fileQuality.setFileName(fileName);
            fileQuality.setFileType(fileType);
            fileQuality.setPlantCode(plantCode);
            fileQuality.setMessage("文件日期格式有误");
            fileQuality.setForecastDate(new Date());
            fileQuality.setPlantType(plantType);
            fileQuality.setId(null);
            fileQuality.setIsFileDelay(0);
            fileQuality.setIsFileEmpty(0);
            fileQuality.setIsFileMissTag(0);
            fileQuality.setIsFileDefect(0);
            fileQuality.setIsFileFormatError(0);
            fileQuality.setIsFileContentError(0);

            return fileQuality;
        }

        //判断文件时间是否为整15分钟
        String minuteTime = fileNameArr[4].substring(2, 4);
        int Wholemin = Integer.parseInt(minuteTime);
        if (!"DQYC".equals(fileType) && Wholemin % 15 != 0) {
            fileQuality.setFileName(fileName);
            fileQuality.setFileType(fileType);
            fileQuality.setPlantCode(plantCode);
            fileQuality.setMessage("文件日期有误");
            fileQuality.setForecastDate(new Date());
            fileQuality.setPlantType(plantType);
            fileQuality.setId(null);
            fileQuality.setIsFileDelay(0);
            fileQuality.setIsFileEmpty(0);
            fileQuality.setIsFileMissTag(0);
            fileQuality.setIsFileDefect(0);
            fileQuality.setIsFileFormatError(0);
            fileQuality.setIsFileContentError(0);

            return fileQuality;
        }


        if (plantType.equals("FD") || plantType.equals("GF")) {
            String[] plantCodeArr = plantCode.split("\\.");
            //String nwCode = fileNameArr[0] + "_" + plantCodeArr[0] + ".";
            if (plantCodeArr.length != 2) {
                result = "场站code有误";
            } else {
                // 场站id
                List<Map<String, Object>> idList = plantDataMapper.selectPlantData(plantCodeArr[1]);
                log.info("场站信息："+Arrays.toString(idList.toArray()));
                if (idList.size() == 0) {
                    result = "没有该场站记录";
                } else if (idList.size() == 1) {
                    plantId = idList.get(0).get("ID").toString();

                    if (fileType.equals("TJXX") || fileType.equals("ZTXX-QXHJ") || fileType.equals("DQYC") || fileType.equals("NBQ-HLX") || fileType.equals("CDQYC")
                            || fileType.equals("SYZXX") || fileType.equals("FDJZ") || fileType.equals("ZTXX-QXHJ-TYGZ") || fileType.equals("XB-FZ")) {

                        result = "success";
                    } else {
                        result = "文件类型不规范";
                    }
                } else {
                    plantId = idList.get(0).get("ID").toString();
                    fileQuality.setId(plantId);
                    result = "场站id不唯一";
                }
            }
        } else {
            result = "场站类型不规范";
        }

        fileQuality.setFileName(fileName);
        fileQuality.setFileType(fileType);
        fileQuality.setPlantCode(plantCode);
        fileQuality.setMessage(result);
        fileQuality.setForecastDate(forecastDate);
        fileQuality.setPlantType(plantType);
        fileQuality.setId(plantId);
        fileQuality.setIsFileDelay(0);
        fileQuality.setIsFileEmpty(0);
        fileQuality.setIsFileMissTag(0);
        fileQuality.setIsFileDefect(0);
        fileQuality.setIsFileFormatError(0);
        fileQuality.setIsFileContentError(0);

        return fileQuality;
    }

    @Override
    public Integer selectFileQualityData(FileQualityEntity fileQuality) {

        return fileQualityMapper.selectFileQuality(fileQuality);
    }

    @Override
    public void insertFileQualityData(FileQualityEntity fileQuality) {
        int num = this.selectFileQualityData(fileQuality);
        fileQuality.setCreateDate(new Date());
        if (num == 0){
            log.info("文件质量-不合格文件-插入----"+fileQuality.getFileName());
            fileQualityMapper.insertFileQuality(fileQuality);
        }else {
            log.info("文件质量-不合格文件-更新----"+fileQuality.getFileName());
            fileQualityMapper.updateFileQuality(fileQuality);
        }


    }

    @Override
    public FileQualityEntity DQYCFileQuality(File file, FileQualityEntity fileQualityEntity) {
        fileQualityEntity = DQYCFileQualityService.DQYCFileQuality(file,fileQualityEntity);
        return fileQualityEntity;
    }

    @Override
    public FileQualityEntity CDQYCFileQuality(File file, FileQualityEntity fileQualityEntity) {
        fileQualityEntity =CDQYCFileQualityService.CDQYCFileQuality(file,fileQualityEntity);
        return fileQualityEntity;
    }

    @Override
    public FileQualityEntity FDJZFileQuality(File file, FileQualityEntity fileQualityEntity) {
        fileQualityEntity =FDJZFileQualityService.FDJZFileQuality(file,fileQualityEntity);
        return fileQualityEntity;
    }

    @Override
    public FileQualityEntity GfNBQ_HLXFileQuality(File file, FileQualityEntity fileQualityEntity) {
        fileQualityEntity =NBQ_HLXFileQualityService.GfNBQ_HLXFileQuality(file,fileQualityEntity);
        return fileQualityEntity;
    }

    @Override
    public FileQualityEntity FdQXHJFileQuality(File file, FileQualityEntity fileQualityEntity) {
        fileQualityEntity =QXHJFileQualityService.FdQXHJFileQuality(file,fileQualityEntity);
        return fileQualityEntity;
    }

    @Override
    public FileQualityEntity GfQXHJFileQuality(File file, FileQualityEntity fileQualityEntity) {
        fileQualityEntity =QXHJFileQualityService.GfQXHJFileQuality(file,fileQualityEntity);
        return fileQualityEntity;
    }

    @Override
    public FileQualityEntity FdSYZXXFileQuality(File file, FileQualityEntity fileQualityEntity) {
        fileQualityEntity =SYZXXFileQualityService.FdSYZXXFileQuality(file,fileQualityEntity);
        return fileQualityEntity;
    }

    @Override
    public FileQualityEntity GfSYZXXFileQuality(File file, FileQualityEntity fileQualityEntity) {
        fileQualityEntity =SYZXXFileQualityService.GfSYZXXFileQuality(file,fileQualityEntity);
        return fileQualityEntity;
    }

    @Override
    public FileQualityEntity FDTJXXFileQuality(File file, FileQualityEntity fileQualityEntity) {
        fileQualityEntity =TJXXFileQualityService.FDTJXXFileQuality(file,fileQualityEntity);
        return fileQualityEntity;
    }

    @Override
    public FileQualityEntity GFTJXXFileQuality(File file, FileQualityEntity fileQualityEntity) {
        fileQualityEntity =TJXXFileQualityService.GFTJXXFileQuality(file,fileQualityEntity);
        return fileQualityEntity;
    }

    @Override
    public FileQualityEntity GfXBFZFileQuality(File file, FileQualityEntity fileQualityEntity) {
        fileQualityEntity =XB_FZFileQualityService.GfXBFZFileQuality(file,fileQualityEntity);
        return fileQualityEntity;
    }


}
